﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Microsoft.Xna.Framework;

/**
 * 创建时间：2014/12/22 17:21:33
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace ComicIsland.Common
{
    public class ApplicationInfoHelper
    {
        public class ApplicationInfo
        {
            public string ApplicationProductId { get; set; }
            public string ApplicationTitle { get; set; }
            public string ApplicationVersion { get; set; }
            public string ApplicationAuthor { get; set; }
            public string ApplicationDescription { get; set; }
            public string ApplicationPublisher { get; set; }
            public string ApplicationPublisherId { get; set; }
        }

        public static ApplicationInfo GetApplicationInfoHelper()
        {
            ApplicationInfo appInfo = new ApplicationInfo();
            try
            {
                using (Stream stream = TitleContainer.OpenStream(KEYS.WMAPPMANIFEST_NAME))
                {
                    XElement xElement = XElement.Load(stream);
                    IEnumerable<XElement> appElements = xElement.Descendants("App");
                    //
                    appInfo.ApplicationProductId = Enumerable.FirstOrDefault<string>(Enumerable.Select<XElement, string>(appElements, (XElement app) => app.Attribute("ProductID").Value));
                    appInfo.ApplicationTitle = Enumerable.FirstOrDefault<string>(Enumerable.Select<XElement, string>(appElements, (XElement app) => app.Attribute("Title").Value));
                    appInfo.ApplicationVersion = Enumerable.FirstOrDefault<string>(Enumerable.Select<XElement, string>(appElements, (XElement app) => app.Attribute("Version").Value));
                    appInfo.ApplicationAuthor = Enumerable.FirstOrDefault<string>(Enumerable.Select<XElement, string>(appElements, (XElement app) => app.Attribute("Author").Value));
                    appInfo.ApplicationDescription = Enumerable.FirstOrDefault<string>(Enumerable.Select<XElement, string>(appElements, (XElement app) => app.Attribute("Description").Value));
                    appInfo.ApplicationPublisher = Enumerable.FirstOrDefault<string>(Enumerable.Select<XElement, string>(appElements, (XElement app) => app.Attribute("Publisher").Value));
                    appInfo.ApplicationPublisherId = Enumerable.FirstOrDefault<string>(Enumerable.Select<XElement, string>(appElements, (XElement app) => app.Attribute("PublisherID").Value));
                    if (!string.IsNullOrWhiteSpace(appInfo.ApplicationPublisherId))
                    {
                        appInfo.ApplicationPublisherId = appInfo.ApplicationPublisherId.Replace("{", "").Replace("}", "");
                    }
                }
            }
            catch (Exception err)
            {
                LogHelper.LogError(err.ToString(), typeof(ApplicationInfoHelper), MethodBase.GetCurrentMethod());
            }
            return appInfo;
        }
    }
}
